package imserver.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ResponseBody;

import imserver.beans.MemberInfo;
import imserver.common.util.BaseResponse;
import imserver.common.util.DaoException;
import imserver.common.util.StringUtil;
import imserver.service.FriendService;

public class FriendController {
	
	@Resource
	private FriendService friendService;
	
	
	/**
	 * 添加一个好友
	 * @return
	 * @throws DaoException 
	 */
	public BaseResponse add(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		int ownerId = Integer.parseInt(request.getParameter("ownerId"));
		String memberNum = request.getParameter("memberNum");
		response.setStatus(200);
		if(ownerId <=0){
			response.setErrorno(-1);
			response.setMsg("用户id不可以少啊！");
		}else if(StringUtil.isNullOrEmpty(memberNum)){
			response.setErrorno(-1);
			response.setMsg("你要加的好友的账号不能少啊！");
		}else{
			friendService.add(ownerId, memberNum);
		}
		return response;
	}
	
	/**
	 * 删除一个好友
	 * @return
	 * @throws DaoException 
	 */
	public BaseResponse del(HttpServletRequest request) throws DaoException{
		BaseResponse response = new BaseResponse();
		int ownerId = Integer.parseInt(request.getParameter("ownerId"));
		int memberId = Integer.parseInt(request.getParameter("memberId"));
		response.setStatus(200);
		if(ownerId <=0){
			response.setErrorno(-1);
			response.setMsg("用户id不可以少啊！");
		}else if(memberId <= 0){
			response.setErrorno(-1);
			response.setMsg("你删除的好友的账号不能少啊！");
		}else{
			friendService.del(ownerId, memberId);
			response.setErrorno(0);
			response.setMsg("ok");
		}
		return response;
	}
	
	/**
	 * 获取所有的好友
	 * @return
	 * @throws DaoException 
	 */
	@ResponseBody
	public List<MemberInfo> list(HttpServletRequest request) throws DaoException{
		int ownerId = Integer.parseInt(request.getParameter("ownerId"));
		if(ownerId >=0){
			return friendService.list(ownerId);
		}else{
			return new ArrayList<MemberInfo>();
		}
		
	}
}
